<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChRoomAutoTagConfig;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;

/**
 * 自动打标签配置
 */
class ChRoomAutoTagConfigMapper extends AbstractMapper
{
    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
       $this->model = ChRoomAutoTagConfig::class;
    }

    /**
     * 通过标签ID查找
     * @param mixed $tagId
     * @return ChRoomAutoTagConfig|Model|string|Builder|null
     */
    #[Cacheable(prefix: 'room_auto_tag_config',value: '#{tagId}', ttl: 15 * 60)]
    public function findByTagId(mixed $tagId): ChRoomAutoTagConfig|Model|string|Builder|null
    {
        return $this->model::query()
            ->where('tag_id',$tagId)
            ->first();
    }

    /**
     * 删除缓存
     * @param mixed $tagId
     * @return void
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function delCache(mixed $tagId): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'room_auto_tag_config:'.$tagId;
        $redis->del([$keyOne]);
    }
}